<template>
  <header class="modern-header">
    <div class="header-container">
      <div class="header-left">
        <router-link to="/" class="site-title">
          <div class="title-chinese">山行者</div>
          <div class="title-english">MountainWalker</div>
        </router-link>
      </div>
      <div class="header-center">
        <nav class="main-nav">
          <ul>
            <li>
              <router-link to="/" class="nav-link" active-class="nav-link-active">主页</router-link>
            </li>
            <li>
              <router-link to="/homepage/scenic" class="nav-link" active-class="nav-link-active">景点</router-link>
            </li>
            <li>
              <router-link to="/homepage/strategy" class="nav-link" active-class="nav-link-active">攻略</router-link>
            </li>
            <li>
              <router-link to="/homepage/rank" class="nav-link" active-class="nav-link-active">榜单</router-link>
            </li>
            <li>
              <router-link to="/homepage/group-tour" class="nav-link" active-class="nav-link-active">跟团游</router-link>
            </li>
          </ul>
        </nav>
      </div>
      <div class="header-right">
        <div class="search-area">
          <input type="text" placeholder="搜索山行者" class="search-input">
          <div class="search-icon">🔍</div>
        </div>
        <div class="user-actions">
          <span @click="goUserCenter" class="user-center-link">用户中心</span>
          <button class="login-btn" v-if="!userInfo" @click="goLogin">你好,请登录!</button>
          <button class="login-btn logged-in" v-else @click="goUserCenter">
            你好,{{ userInfo.username || '用户' }}
          </button>
        </div>
      </div>
    </div>
  </header>
  <main>
    <router-view></router-view>
  </main>
</template>

<script>
export default {
  name: 'IndexFrame',
  data() {
    return {
      userInfo: null
    }
  },
  mounted() {
    const loginId = localStorage.getItem('loginId');
    if (loginId) {
      // 通过loginId查username
      fetch(`http://localhost:8080/mw/UserByLoginId?loginId=${loginId}`)
        .then(res => res.json())
        .then(user => {
          this.userInfo = { username: user.userName };
        })
        .catch(() => {
          this.userInfo = null;
        });
    }
  },
  methods: {
    goUserCenter() {
      if (!this.userInfo) {
        this.$message && this.$message.warning
          ? this.$message.warning('请登录！')
          : alert('请登录！');
      } else {
        this.$router.push('/userframe/usermain');
      }
    },
    goLogin() {
      this.$router.push('/loginpage');
    }
  }
}
</script>

<style scoped>
/* 现代化导航栏样式 */
.modern-header {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 90px;
  background: transparent;
  z-index: 1000;
  transition: all 0.3s ease;
}

.modern-header::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 40px;
  right: 40px;
  height: 1px;
  background: rgba(255, 255, 255, 0.3);
  transition: all 0.3s ease;
}

.header-container {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 100%;
  max-width: 1400px;
  margin: 0 auto;
  padding: 0 40px;
}

/* 网站标题 */
.header-left .site-title {
  text-decoration: none;
  transition: all 0.3s ease;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  line-height: 1;
}

.title-chinese {
  font-size: 3.2rem;
  font-weight: 200;
  color: white;
  letter-spacing: 2px;
  margin-bottom: 8px;
}

.title-english {
  font-size: 2.5rem;
  font-weight: 300;
  color: rgba(255, 255, 255, 0.9);
  font-family: 'Brush Script MT', 'Lucida Handwriting', cursive;
  font-style: italic;
  letter-spacing: 1px;
  margin-left: 8px;
}

.header-left .site-title:hover .title-chinese,
.header-left .site-title:hover .title-english {
  opacity: 0.8;
}

/* 中间导航 */
.header-center {
  flex: 1;
  display: flex;
  justify-content: center;
}

.main-nav ul {
  display: flex;
  list-style: none;
  margin: 0;
  padding: 0;
  gap: 60px;
}

.main-nav li {
  margin: 0;
}

.nav-link {
  color: white;
  text-decoration: none;
  font-size: 1.4rem;
  font-weight: 300;
  letter-spacing: 1px;
  position: relative;
  padding: 10px 0;
  transition: all 0.3s ease;
}

.nav-link:hover {
  opacity: 0.7;
}

.nav-link-active {
  opacity: 1;
}

.nav-link::after {
  content: '';
  position: absolute;
  bottom: -2px;
  left: 50%;
  width: 0;
  height: 2px;
  background: white;
  transition: all 0.3s ease;
  transform: translateX(-50%);
}

.nav-link:hover::after,
.nav-link-active::after {
  width: 100%;
}

/* 右侧区域 */
.header-right {
  display: flex;
  align-items: center;
  gap: 25px;
}

/* 搜索框 */
.search-area {
  position: relative;
  display: flex;
  align-items: center;
}

.search-input {
  background: rgba(255, 255, 255, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.3);
  border-radius: 30px;
  padding: 14px 45px 14px 22px;
  width: 260px;
  color: white;
  font-size: 1.1rem;
  font-weight: 300;
  outline: none;
  transition: all 0.3s ease;
}

.search-input::placeholder {
  color: rgba(255, 255, 255, 0.6);
}

.search-input:focus {
  background: rgba(255, 255, 255, 0.15);
  border-color: rgba(255, 255, 255, 0.5);
  width: 300px;
}

.search-icon {
  position: absolute;
  right: 18px;
  color: rgba(255, 255, 255, 0.6);
  font-size: 1.3rem;
  pointer-events: none;
}

/* 用户操作区域 */
.user-actions {
  display: flex;
  align-items: center;
  gap: 20px;
}

.user-center-link {
  color: white;
  font-size: 1.2rem;
  font-weight: 300;
  cursor: pointer;
  transition: all 0.3s ease;
  letter-spacing: 0.5px;
}

.user-center-link:hover {
  opacity: 0.7;
}

.login-btn {
  background: transparent;
  border: 1px solid rgba(255, 255, 255, 0.4);
  color: white;
  padding: 12px 24px;
  border-radius: 30px;
  font-size: 1.1rem;
  font-weight: 300;
  cursor: pointer;
  transition: all 0.3s ease;
  white-space: nowrap;
  letter-spacing: 0.5px;
}

.login-btn:hover {
  background: rgba(255, 255, 255, 0.1);
  border-color: rgba(255, 255, 255, 0.6);
}

.login-btn.logged-in {
  background: rgba(255, 255, 255, 0.1);
  border-color: rgba(255, 255, 255, 0.3);
}

/* 响应式设计 */
@media (max-width: 1200px) {
  .header-container {
    padding: 0 30px;
  }
  
  .main-nav ul {
    gap: 35px;
  }
  
  .search-input {
    width: 180px;
  }
  
  .search-input:focus {
    width: 200px;
  }
}

@media (max-width: 992px) {
  .header-container {
    padding: 0 20px;
  }
  
  .main-nav ul {
    gap: 25px;
  }
  
  .nav-link {
    font-size: 1rem;
  }
  
  .search-input {
    width: 160px;
  }
  
  .search-input:focus {
    width: 180px;
  }
}

@media (max-width: 768px) {
  .modern-header::after {
    left: 20px;
    right: 20px;
  }
  
  .modern-header {
    height: auto;
    min-height: 90px;
  }
  
  .header-container {
    flex-direction: column;
    padding: 20px;
    gap: 20px;
  }
  
  .header-left {
    order: 1;
  }
  
  .header-center {
    order: 3;
    width: 100%;
  }
  
  .header-right {
    order: 2;
    width: 100%;
    justify-content: space-between;
  }
  
  .main-nav ul {
    gap: 35px;
    flex-wrap: wrap;
    justify-content: center;
  }
  
  .nav-link {
    font-size: 1.2rem;
  }
  
  .search-area {
    flex: 1;
  }
  
  .search-input {
    width: 100%;
    max-width: 240px;
  }
  
  .search-input:focus {
    width: 100%;
  }
}

@media (max-width: 480px) {
  .modern-header::after {
    left: 15px;
    right: 15px;
  }
  
  .header-container {
    padding: 12px 15px;
  }
  
  .title-chinese {
    font-size: 2.4rem !important;
  }
  
  .title-english {
    font-size: 1.2rem !important;
  }
  
  .main-nav ul {
    gap: 20px;
  }
  
  .nav-link {
    font-size: 0.9rem;
  }
  
  .user-actions {
    gap: 15px;
    flex-wrap: wrap;
  }
  
  .login-btn {
    font-size: 0.85rem;
    padding: 8px 16px;
  }
}

/* 页面内容区域适配 */
main {
  padding-top: 0px;
}

@media (max-width: 768px) {
  main {
    padding-top: 160px;
  }
}

@media (max-width: 480px) {
  main {
    padding-top: 180px;
  }
}
</style>